<?php
class newsTypeController extends PublicController
{
    public function getListAction(){
        $ob = D('newsType');
        $page = isset($_GET['page'])?$_GET['page']:1;
        $page_num = 5;
        $where = '';
        $data = $ob->getPageTypeList($page,$page_num,$where);
        $count = $data['count'];
        $page_ob = new page($count, $page,$page_num);
        $page_str = $page_ob->getPageBlock(2);
        $this->assign('page_str', $page_str);
        $this->assign('data', $data['value']);
        $this->display('type_list.html');
    }
    public function addAction(){
        $ob = D('newsType');
        if(!empty($_POST)){
            $name = $_POST['name'];
            if(empty($name)){
                $this->success(APP.'/newsType/add.html', '类别名称不能为空');
            }
            $arr=array(
                'name'=>$_POST['name'],
                'remark'=>$_POST['remark'],
                'order_num'=>$_POST['order_num'],
                'create_time'=>time()
            );
            $re = $ob->insert($arr);
            if($re){
                $this->success(APP.'/newsType/getList.html', '添加类别成功');
            }else {
                $this->success(APP.'/newsType/add.html', '添加类别失败');
            }
        }else {
            $this->display('type_add.html');
        }
    }
    public function delAction(){
        $ob = D('newsType');
        $tid = isset($_GET['tid']) ? $_GET['tid'] : '';
        $re = $ob->where("tid={$tid}")->delete();
        if($re){
            $this->redirect(APP.'/newsType/getList.html','删除成功');
        }else{
            $this->redirect(APP.'/newsType/getList.html','删除失败');
        }
    }
    public function editAction(){
        $ob = D('newsType');
        $tid = get('tid');
        if(empty($_POST)){
            $data = $ob->getTypeById($tid);
            if(empty($data)){
                $this->redirect(APP.'/newsType/getList.html','该类型不存在');
            }else {
                $this->assign('data', $data);
                $this->display('type_edit.html');
            }
        }else {
            $name = $_POST['name'];
            if(empty($name)){
                $this->success(APP."/newsType/edit-tid-$tid.html", '类别名称不能为空');
            }
            $arr=array(
                'name'=>$_POST['name'],
                'remark'=>$_POST['remark'],
                'order_num'=>$_POST['order_num'],
            );
            $re = $ob->where("tid='{$tid}'")->update($arr);
            if($re){
                $this->success(APP.'/newsType/getList.html', '编辑类型成功');
            }else {
                $this->success(APP."/newsType/edit-tid-$tid.html", '编辑类型失败');
            }
        }
    }
}